﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using eschoolBO;

namespace ESCHOOL
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        Classes cl = new Classes();
        student stud = new student();
        report rep = new report();
        reportcard rc = new reportcard(); subject sub = new subject();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = cl.GetRecords(-99, "", -99, false, " and ");
                ddclass.DataSource = dt;
                ddclass.DataTextField = "class_name";
                ddclass.DataValueField = "class_id";
                ddclass.DataBind();
            }
        }

        protected void btnok_Click(object sender, EventArgs e)
        {
            rc.deletetable("delete from reportcard");
            DataTable dt = stud.GetRecords(-99, "", Convert.ToInt64(tbrollno.Text), Convert.ToInt64(ddclass.SelectedValue.ToString()), "", "", "", "", "", "", -99, -99, "", -99, "", "", "", "", false, " and ");
            DataTable dtr1 = rep.GetRecords(-99, "annual", Convert.ToInt64(ddclass.SelectedValue.ToString()), Convert.ToInt64(dt.Rows[0]["student_id"].ToString()), -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, "", -99, false, " and ");
            DataTable dtr2 = rep.GetRecords(-99, "quarterly", Convert.ToInt64(ddclass.SelectedValue.ToString()), Convert.ToInt64(dt.Rows[0]["student_id"].ToString()), -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, "", -99, false, " and ");
            DataTable dtr3 = rep.GetRecords(-99, "half yearly", Convert.ToInt64(ddclass.SelectedValue.ToString()), Convert.ToInt64(dt.Rows[0]["student_id"].ToString()), -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, "", -99, false, " and ");
            for (int j = 0; j < dtr1.Rows.Count ; j++)
            {
                DataTable dt2 = sub.GetRecords(Convert.ToInt64(dtr1.Rows[j]["subject_id"].ToString()), "", -99, -99, false, " and ");
            }
            if (dtr1.Rows.Count > 0)
            {
                for (int i = 0; i < dtr1.Rows.Count; i++)
                {
                    rc.r_id = -99;
                    rc.student_name = dt.Rows[0]["student_name"].ToString();
                    rc.subject_name = dtr2.Rows[i]["subject_name"].ToString();
                    rc.a_max = Convert.ToInt16(dtr1.Rows[i]["total_maxmarks"].ToString());
                    rc.a_obt = Convert.ToInt16(dtr1.Rows[i]["total_obtmarks"].ToString());
                    rc.q_max = Convert.ToInt16(dtr2.Rows[i]["total_maxmarks"].ToString());
                    rc.q_obt = Convert.ToInt16(dtr2.Rows[i]["total_obtmarks"].ToString());
                    rc.hy_max = Convert.ToInt16(dtr3.Rows[i]["total_maxmarks"].ToString());
                    rc.hy_obt = Convert.ToInt16(dtr3.Rows[i]["total_obtmarks"].ToString());
                    rc.total = Convert.ToInt16(dtr1.Rows[i]["total_obtmarks"].ToString()) + Convert.ToInt16(dtr2.Rows[i]["total_obtmarks"].ToString()) + Convert.ToInt16(dtr3.Rows[i]["total_obtmarks"].ToString());
                    rc.percentage = (Convert.ToDouble(dtr1.Rows[i]["percentage"].ToString()) + Convert.ToDouble(dtr2.Rows[i]["percentage"].ToString()) + Convert.ToDouble(dtr3.Rows[i]["percentage"].ToString())) / 3.0;
                    if (Convert.ToDouble(dtr1.Rows[i]["percentage"].ToString()) + Convert.ToDouble(dtr2.Rows[i]["percentage"].ToString()) + Convert.ToDouble(dtr3.Rows[i]["percentage"].ToString())/3.0 < 40)
                        rc.grade = "C";
                    else if (Convert.ToDouble(dtr1.Rows[i]["percentage"].ToString()) + Convert.ToDouble(dtr2.Rows[i]["percentage"].ToString()) + Convert.ToDouble(dtr3.Rows[i]["percentage"].ToString())/3.0 < 60)
                        rc.grade = "B";
                    else
                        rc.grade = "A";
                    rc.SaveRecords();
                }
            }

            Response.Redirect("~/showresult.aspx");
        }
    }
}